package portal.mybatis.dao.service;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import portal.beans.Gpu;
import portal.mybatis.dao.NodeDetail;
import portal.mybatis.dao.mapper.GpuMapper;
import portal.mybatis.page.Page;

@Service
public class GpuService {
	@Autowired
	GpuMapper gpuMapper;
	public long countAll(){
		return gpuMapper.countAll();
	}
	public boolean insertGpu(String identify,String version,String detail){
		if(identify==null ||identify .length()==0)
			return  false;
		Gpu g=new Gpu();
		g.setCreateTime(new Date(System.currentTimeMillis()));
		g.setGpuDetail(detail);
		g.setGpuIdentify(identify);
		g.setGpuVersion(version);
		return insertGpu(g);
	}
	public boolean insertGpu(Gpu gpu){
		if(gpu ==null || gpu.getGpuIdentify()==null)return false;
		return gpuMapper.insertGpu(gpu)>0;
	}
	public boolean updateGpu(String identify,String version,String detail,Integer id){
		Gpu g=new Gpu();
		g.setGpuId(id);
		g.setGpuDetail(detail);
		g.setGpuIdentify(identify);
		g.setGpuVersion(version);
		return updateGpu(g);
	}
	public boolean updateGpu(Gpu g){
		if(g ==null)return false;
		return gpuMapper.updateGpuById(g)>0; 
	}
	/***
	 * Gpu信息不允许删除
	 * **/
	public boolean deleteGpu(Integer id){
		return true;
	}
	public Page<Gpu> getPage(Page<Gpu>page){		
		List<Gpu>list =gpuMapper.selectByPage(page);	
		if(list==null || list.size()==0){
			return null;
		}
		page.setRows(list);
		page.setTotal(gpuMapper.countByPage(page));
		return page;
	}
	public Gpu selectGpuById(Integer id){
		return selectOneByOr(id,null,null);
	}
	public Gpu selectGpuByKey(String key){
		return selectOneByOr(null,key,key);
	}
	public Gpu selectByName(String identify){
		return selectOneByOr(null,identify,null);
	}
	public Gpu selectByVersion(String version){
		return selectOneByOr(null,null,version);
	}
	public Gpu selectOneByOr(Integer gpuId ,String identify, String version){
		if(gpuId==null && identify==null && version ==null)
			return null;//此处检测保证只取一个
		List<Gpu> list= selectAllByOr(gpuId,identify,version);
		if(list==null || list.size()==0)
			return null;
		return list.get(0);
	}
	public List<Gpu> selectAllByOr(Integer gpuId ,String identify, String version){
//		System.out.println("gpuId " + gpuId);
//		System.out.println("gpuId " + identify);
//		System.out.println("gpuId " + version);
		if(gpuId==null && identify==null && version ==null)//此处检测保证为空时候，获取所有GPU 信息
			return gpuMapper.selectByOr(null);
		Gpu gpu =new Gpu();
		if(gpuId!=null)
			gpu.setGpuId(gpuId);
		if(identify!=null)
			gpu.setGpuIdentify(identify);
		if(version!=null)
			gpu.setGpuVersion(version);
		List<Gpu> list= gpuMapper.selectByOr(gpu);
		if(list==null || list.size()==0)
			return null;
		return list;
	}
}
